/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trafficsim;

import java.io.*;

/**
 *
 * @author Ecca
 */
public class UI {

    private Model proj;
    private String input;
    private int lanes;
    private double probability;
    private double prv;
    private double prh;

    /**
     * constructor
     */
    public UI() {
        proj = new Model();
    }

    /**
     * shows the string menu
     */
    public void showMenu() {
        System.out.println("==== MENU ====\n"
                + "1. Set Number of horizontal lanes (H-street) [min 1, max 3]\n"
                + "2. Set Number of vertical lanes (V-street) [min 1, max 4]\n"
                + "3. Set Probability of a car entering H-street [min 0, max 1]\n"
                + "4. Set Probability of a car entering V-street [min 0, max 1]\n"
                + "5. Run one simulation cycle\n"
                + "6. Set and run number of simulation cycles [min 1, max 10]\n");
    }

    /**
     * displays the menu, gets the input, updates the grid.
     * @throws IOException
     * 
     */
    public void run() throws IOException {

        while (true) {
            View gv = new View(proj);
            gv.show();
            showMenu();
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            String choice = "";
            System.out.print("Enter your choice> ");
            choice = br.readLine();
            if (choice.isEmpty()) {
                System.out.println("Please enter something");
            }

            if (choice.equals("1")) {
                while (true) {
                    System.out.print("Enter the number of horizontal lanes: ");
                    input = br.readLine();
                    if (input.equals("1") || input.equals("2") || input.equals("3")) {
                        lanes = Integer.parseInt(input);
                        proj.sethLanes(lanes);
                        break;
                    } else {
                        System.out.println("Please enter a number between 1 and 3");
                    }
                }
            }
            if (choice.equals("2")) {
                while (true) {
                    System.out.print("Enter the number of verticle lanes: ");
                    input = br.readLine();
                    if (input.equals("1") || input.equals("2") || input.equals("3") || input.equals("4")) {
                        lanes = Integer.parseInt(input);
                        proj.setvLanes(lanes);
                        break;
                    } else {
                        System.out.println("Please enter a number between 1 and 4");
                    }
                }
            }
            if (choice.equals("5")) {
                gv.moveCars(1);
            }


            if (choice.equals("3")) {
                verif:
                while (true) {
                    try {
                        System.out.print("\nEnter the probability of a car entering H-street (any double between 0 and 1): ");
                        input = br.readLine();
                        probability = Double.parseDouble(input);
                        if (probability >= 0 && probability <= 1) {
                            prh = probability;
                        } else {
                            System.out.println("Please enter a double between 0 and 1!");
                            continue verif;
                        }

                    } catch (NumberFormatException ex) {
                        System.out.println("Please enter a floating point value!");
                        continue verif;
                    }
                    break;
                }


                if (choice.equals("4")) {
                    verif:
                    while (true) {
                        try {
                            System.out.print("\nEnter the probability of a car entering V-street (any double between 0 and 1): ");
                            input = br.readLine();
                            probability = Double.parseDouble(input);
                            if (probability >= 0 && probability <= 1) {
                                prv = probability;
                            } else {
                                System.out.println("Please enter a double between 0 and 1!");
                                continue verif;
                            }

                        } catch (NumberFormatException ex) {
                            System.out.println("Please enter a floating point value!");
                            continue verif;
                        }
                        break;
                    }
                }
            }
        }
    }
}
